#define PI 3.14159265359
precision highp float;

uniform vec4 color;
uniform float speed;
float radius = 1.;

float glowStrength = .5;
float fillStrength = 0.1;

float d2y(float d) {
    return 1. / (0.2 + d);
}

float fct(vec2 p, float r) {
  float time = czm_frameNumber * speed /1000.;
    float a = 3.0 * mod(-atan(p.y, p.x) + time, 2.0 * PI);
    return (d2y(a) + fillStrength) * (1.0 - step(radius, r));
}

float circle(vec2 p, float r) {
    float d = distance(r, radius);
    return d2y(100.0 * d);
}


czm_material czm_getMaterial(czm_materialInput materialInput){
  czm_material material = czm_getDefaultMaterial(materialInput);
  vec2 vUv = materialInput.st * 2.0 - 1.0;
  float t = czm_frameNumber * speed / 100.0 ;
  vec2 position =  vec2(vUv.x , vUv.y );
  // vec2 position = vUv.xy;
  // position /= cos(1.5 * length(position));
  float y = 0.0;
  float dc = length(position);
  y += fct(position, dc);
  y += circle(position, dc);
  y = pow(y, 1.0 / glowStrength );
  vec4 FragColor = vec4(sqrt(y) * color.rgb, 1.0);

  material.alpha  = pow(y ,.7) * color.a * .8;
  material.diffuse = FragColor.rgb * color.rgb * 0.6;
  return material;
}